<?php

namespace app\utils\ui\native_element;

use app\utils\ui\Script;
use app\utils\ui\UIView;
use app\utils\ui\VueView;

class Root {

    /**
     * @var UIView
     */
    public $child;

    public function __construct(UIView $child) 
    {
        $this->child=$child;
    }

    /**
     * 返回最终HTML
     * @param bool $end_html  true 输出 html尾部 
     */
    public function render($end_html=true,$title='',$style_html='',$style_path=[]){
        $out_style_html2='';
        foreach($style_path as $path){
            $out_style_html2.='<link rel="stylesheet" href="'.$path.'">\r\n';
        }
        $html='
        <!DOCTYPE html>
        <html lang="en">
        <head>
            <meta charset="UTF-8">
            <meta http-equiv="X-UA-Compatible" content="IE=edge">
            <meta name="viewport" content="width=device-width, initial-scale=1.0">
            <title>'.$title.'</title>
            <style>'.$style_html.'</style>
            '.$out_style_html2.'
        </head>
        <body>
            '.$this->child->render();
        if($end_html){
            $html.='
            </body>
            </html>
            ';
        }
        return $html;
    }
    
    /**
     * @var array 数据
     */
    private $data=array();

    /**
     * @param array $other_data 不能将该值设置成数组以外的内容，默认 []，这部分的data以最低优先级合并到data中，在UI中的var会覆盖掉other_data 所定义的。
     */
    public function data($other_data=[])
    {
        $this->data = array_merge($other_data,$this->data,$this->child->data());
        return $this->data;
    }

    /**
     * @var array 函数方法
     */
    public $methods=array();

    public function method()
    {
        if($this->child instanceof VueView){
            $this->methods = array_merge($this->methods,$this->child->method());
            return $this->methods;
        }else{
            if(method_exists($this->child,'method')){
                $method = 'method';
                $this->methods = array_merge($this->methods,$this->child->$method());
                return $this->methods;
            }
            return array();
        }
    }

    /**
     * 输出script HTML
     * @param bool $end_html 是否输出 html尾部
     */
    public function script(Script $script,$end_html=true){
        $html = $script->script();
        if($end_html){
            $html.='
            </body>
            </html>
            ';
        }
        return $html;
    }
    
}